#! /bin/csh -f

if !(-d $CASEBUILD) mkdir $CASEBUILD
if !(-d $CASEBUILD/ciceconf) mkdir $CASEBUILD/ciceconf

#--------------------------------------------------------------------
# Invoke cice configure
#--------------------------------------------------------------------


set hgrid = "-hgrid $ICE_GRID"
if ($ICE_GRID =~ *T*) set hgrid = "-hgrid ${ICE_NX}x${ICE_NY}"

set mode = "-cice_mode $CICE_MODE"

cd $CASEBUILD/ciceconf || exit -1
$CODEROOT/ice/cice/bld/configure $hgrid $mode -nodecomp $CICE_CONFIG_OPTS || exit -1

#----------------------------
# Create ccsm_namelist file
#----------------------------

set ice_ic = 'default'
if ($RUN_TYPE == startup) then
  if ($ICE_GRID =~ *gx* && $CICE_MODE != prescribed) then
     set ice_ic = \$DIN_LOC_ROOT/ice/cice/iced.0001-01-01.${ICE_GRID}_20080212
  endif 
else if ($RUN_TYPE == branch || $RUN_TYPE == hybrid) then
  set ice_ic = ${RUN_REFCASE}.cice.r.${RUN_REFDATE}-00000.nc
endif

cat >! $CASEBUILD/ciceconf/ccsm_namelist << EOF1
 &ice_nml
 ice_ic = '$ice_ic'
 /
 &domain_nml
 distribution_type = 'DECOMPTYPE'
 / 
EOF1

# Allow the user to supply namelist values by putting them in the file
# $CASEROOT/user_nl_cice.  The file should contain a valid namelist, just like
# any file that supplies user input to build-namelist via the -infile argument.
if (-f $CASEROOT/user_nl_cice) then
   cat $CASEROOT/user_nl_cice >> $CASEBUILD/ciceconf/ccsm_namelist || exit -1
endif

#----------------------------
# Invoke build-namelist
#----------------------------

set SSTDATA = "FALSE"
if ($CICE_MODE == "prescribed") then
   if (! $?DOCN_SSTDATA_FILENAME) then
      echo "DOCN_SSTDATA_FILENAME must be defined for cice prescribed mode"
      exit -1
   endif  
   if ($DOCN_SSTDATA_FILENAME   != 'UNSET' && \
       $DOCN_SSTDATA_YEAR_START != '-999'  && \
       $DOCN_SSTDATA_YEAR_END   != '-999' ) then
       set SSTDATA = "TRUE"
   endif
   set CCSMSSTFN = $DOCN_SSTDATA_FILENAME
   if ($SSTDATA == 'TRUE') then
      if ($DOCN_SSTDATA_FILENAME =~ $DIN_LOC_ROOT*) then
         set num = `echo $DIN_LOC_ROOT | wc -c `
         set dd = `echo $DOCN_SSTDATA_FILENAME | cut -c $num-`
         if ($dd =~ /*) set dd = `echo $dd | cut -c 2-`
         set CCSMSSTFN = '$DIN_LOC_ROOT'"/$dd"
      endif
   endif
endif

if ($SSTDATA == "TRUE") then
  $CODEROOT/ice/cice/bld/build-namelist -config config_cache.xml \
        -csmdata \$DIN_LOC_ROOT -infile ccsm_namelist \
        -inputdata $CASEBUILD/cice.input_data_list \
        -namelist "&cice $CICE_NAMELIST_OPTS \
                    stream_fldfilename='$CCSMSSTFN' \
                    stream_domfilename='$CCSMSSTFN' \
                    stream_year_first=$DOCN_SSTDATA_YEAR_START \
                    stream_year_last=$DOCN_SSTDATA_YEAR_END \
                    model_year_align=$DOCN_SSTDATA_YEAR_START \
                    stream_fldvarname='ice_cov' /"  || exit -1
else
  $CODEROOT/ice/cice/bld/build-namelist -config config_cache.xml \
         -csmdata \$DIN_LOC_ROOT -infile ccsm_namelist \
         -inputdata $CASEBUILD/cice.input_data_list \
         -namelist "&cice $CICE_NAMELIST_OPTS /" || exit -1
endif

sed -e 's#DECOMPTYPE#\$CICE_DECOMPTYPE#' ice_in >! ice_in.tmp || exit 3
mv ice_in.tmp ice_in

#----------------------------
# Append ice_ic to cice.input_data_list
#----------------------------

if ($RUN_TYPE != 'branch' && $RUN_TYPE != 'hybrid' && ${ice_ic} != 'default') then
cat >> $CASEBUILD/cice.input_data_list << EOF1
ice_ic = ${ice_ic}
EOF1
endif

#----------------------------
# Create cice.buildnml.csh
#----------------------------

cat >! $CASEBUILD/cice.buildnml.csh << EOF1
#! /bin/csh -f 

set exedir = \$RUNDIR; cd \$exedir

cat >! ice_in << EOF
EOF1
cat ice_in >> $CASEBUILD/cice.buildnml.csh || exit -1
cat >> $CASEBUILD/cice.buildnml.csh << EOF1
EOF
EOF1

# Delete copy of ice_in.

/bin/rm -f $CASEBUILD/ciceconf/ice_in

#--------------------------------------------------------------------
# Create cice.buildexe.csh
#--------------------------------------------------------------------

cat >! $CASEBUILD/cice.buildexe.csh << EOF2
#! /bin/csh -f 

set objdir = \$OBJROOT/ice/obj; cd \$objdir

set comp = "unknown"
if (\$COMP_INTERFACE == 'MCT' ) set comp = mct
if (\$COMP_INTERFACE == 'ESMF') set comp = esmf

\cat >! .tmp << EOF; cmp -s .tmp Filepath || mv -f .tmp Filepath 
EOF2

cat $CASEBUILD/ciceconf/Filepath >> $CASEBUILD/cice.buildexe.csh

cat >> $CASEBUILD/cice.buildexe.csh << EOF2
EOF

# Check for recompile if BLCKX, BLCKY or MXBLCKS changes
#-------------------------------------------------------
set recompile = FALSE
echo \${CICE_BLCKX} \${CICE_BLCKY} \${CICE_MXBLCKS} > \$objdir/iceres.new
cmp -s \$objdir/iceres.new \$objdir/iceres.old || set recompile = TRUE
if (\$recompile == 'TRUE') then
  cat \$objdir/iceres.old
  cat \$objdir/iceres.new
  echo "blckx,blcky,mxblcks has changed, removing objdir and cice, preparing for new compile"
  rm -f \$objdir/*.o
  rm -f \$objdir/*.f
  rm -f \$objdir/*.f90
endif

# Build the library
#-------------------------------------------------------
set cicedefs = "`cat \$CASEBUILD/ciceconf/CICE_cppdefs`"
set cicedefs = "\$cicedefs -DBLCKX=\$CICE_BLCKX -DBLCKY=\$CICE_BLCKY -DMXBLCKS=\$CICE_MXBLCKS"        
gmake complib -j \$GMAKE_J MODEL=cice COMPLIB=\$LIBROOT/libice.a MACFILE=\$CASEROOT/Macros.\$MACH USER_CPPDEFS="\$cicedefs" -f \$CASETOOLS/Makefile || exit 2

mv \$objdir/iceres.new \$objdir/iceres.old

wait 
exit 0
EOF2

